<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="10">
        <el-col :span="10">
          <el-input placeholder="请输入内容" suffix-icon="el-icon-search" v-model="query.search"
            @keypress.native.enter="getUserList"></el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="addDialogFormVisible = true">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableData.userList" stripe style="width: 100%">
        <el-table-column prop="id" label="id" width="180">
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="address" label="地址">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="query.currentPage" :page-sizes="[2, 4]" :page-size="2" layout="total, sizes, prev, pager, next"
        :total="tableData.total">
      </el-pagination>
    </el-card>
    <!-- 添加用户 -->
    <el-dialog title="添加用户" :visible.sync="addDialogFormVisible">
      <el-form ref="addFormRef" :model="userForm" label-width="80px" :rules="userFormRules">
        <el-form-item label="ID" prop="id">
          <el-input v-model="userForm.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userForm.name"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="userForm.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑用户 -->
    <el-dialog title="编辑用户" :visible.sync="editDialogFormVisible">
      <el-form ref="editFormRef" :model="userForm" label-width="80px" :rules="userFormRules">
        <el-form-item label="ID" prop="id">
          <el-input v-model="userForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userForm.name"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="userForm.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData: {
          userList: [],
          total: 0
        },
        query: {
          currentPage: 1,
          pageSize: 2,
          search: ''
        },
        //添加用户
        addDialogFormVisible: false,
        //编辑用户
        editDialogFormVisible: false,
        userForm: {
          id: 0,
          name: '',
          address: ''
        },
        userFormRules: {
          name: [{
              required: true,
              message: '请输入用户姓名',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 5,
              message: '长度在 3 到 5 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    created() {
      this.getUserList()
    },
    methods: {
      async getUserList() {
        const {
          data: res
        } = await this.$axios.post('/getUserList', this.query)
        // console.log(res)
        this.tableData.userList = res.result
        // console.log(res)
        this.tableData.total = res.total
      },
      //分页
      handleSizeChange(val) {
        this.query.pageSize = val
        this.getUserList()
      },
      handleCurrentChange(val) {
        this.query.currentPage = val
        this.getUserList()
      },
      //添加用户
      addUser() {
        this.$refs.addFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$axios.post('/addUser', this.userForm)
          if (res.code != 200) return this.$message.error('添加用户失败')
          //关闭模态框
          this.addDialogFormVisible = false
          //重新请求最新数据刷新页面
          this.getUserList()
          this.$message.success('添加用户成功')
        })
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {
            data: res
          } = await this.$axios.post('/delUser', {
            id: row.id
          })
          if (res.code == 200)
            this.getUserList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      async handleEdit(row) {
        const {
          data: res
        } = await this.$axios.get('/getUserById', {
          data: {
            id: row.id
          }
        })

        this.userForm = res
        this.editDialogFormVisible = true
      },
      editUser() {
        this.$refs.editFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$axios.post('/editUser', this.userForm)
          if (res.code != 200) return this.$message.error('编辑用户失败')
          //关闭模态框
          this.editDialogFormVisible = false
          //重新请求最新数据刷新页面
          this.getUserList()
          this.$message.success('编辑用户成功')
        })
      }
    }
  }
</script>

<style scoped="scoped">
  .el-card {
    margin-top: 10px;
  }

  .el-pagination {
    margin-top: 10px;
  }
</style>
